// ===================================
// <copyright>
// This file is part of LABSMFController.
//
//    LABSMFController is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    LABSMFController is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with LABSMFController.  If not, see <http://www.gnu.org/licenses/>.
//    Copyright 2011 David Bakker
//
//</copyright>
// <author>David Bakker</author>
// <email>mailto:DavidFBakker@gmail.com</email>
// <created>Saturday, May 07, 2011</created>
// <lastedit>Saturday, May 07, 2011</lastedit>
// ===================================

using System;
using System.Threading;
using LABSMFController.UI.Modules;
using Microsoft.SPOT;
using Microsoft.SPOT.Input;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;

namespace LABSMFController.UI
{
    public class origMainWindow : Window
    {
        private static readonly Font displayFont = Resources.GetFont(Resources.FontResources.NinaB);
        private readonly StackPanel imagePanel = new StackPanel(Orientation.Horizontal);
        private readonly Panel mainPanel = new Panel();
        private readonly Targets myTarget = new Targets();
        private readonly TargetButtons[] myTargetButtons = new TargetButtons[Targets.numberOfTargetButtons];
        private readonly StackPanel target1 = new StackPanel(Orientation.Vertical);
        private readonly StackPanel target2 = new StackPanel(Orientation.Vertical);
        private readonly Text[] targetCount = new Text[Targets.numberOfTargetButtons];
        private readonly Text[] targetLabels = new Text[Targets.numberOfTargetButtons];
        private readonly Color textColor = Colors.Purple;
        private Window mainWindow;

        public origMainWindow()
        {
            Width = SystemMetrics.ScreenWidth;
            Height = SystemMetrics.ScreenHeight;
            Background = new ImageBrush(Resources.GetBitmap(Resources.BitmapResources.BB_Back)) {Stretch = Stretch.None};

            Dispatcher.Thread.Priority = ThreadPriority.Highest;


            var mainCanvas = new ModulesCanvas
                                 {
                                     Width = SystemMetrics.ScreenWidth,
                                     Height = SystemMetrics.ScreenHeight
                                 };

            mainCanvas.AddModule(typeof (TemperatureUIModule));
            mainPanel.Children.Add(mainCanvas);
            //   Child = mainCanvas;

            Visibility = Visibility.Visible;
            Child = mainPanel;

            Buttons.Focus(mainPanel);
        }

        /// <summary>
        /// Set display parameters for the status text.
        /// </summary>		
        //private void AddStatusText(String textToDisplay, Font displayFont, Color textColor, Boolean endOfLine)
        //{
        //    try
        //    {
        //        statusText.TextRuns.Add(textToDisplay, displayFont, textColor);
        //        if (endOfLine)
        //        {
        //            statusText.TextRuns.Add(TextRun.EndOfLine);
        //        }
        //    }
        //    catch (Exception ex)
        //    {
        //        Debug.Print(ex.Message);
        //    }
        //}
        ///// <summary>
        ///// Clear the statusText TextRuns object.
        ///// </summary>		
        //private void ClearStatusText()
        //{
        //    try
        //    {
        //        statusText.TextRuns.Clear();
        //    }
        //    catch (Exception ex)
        //    {
        //        Debug.Print(ex.Message);
        //    }
        //}
        private void SetDisplayParametersForStatusText()
        {
            try
            {
                // ClearStatusText();

                //// statusText.SetMargin(4);
                //// statusText.VerticalAlignment = VerticalAlignment.Bottom;
                // AddStatusText("To record a sighting, touch an image.", displayFont, textColor, true);
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }

        /// <summary>
        /// Respond to a touch on a target image.
        /// </summary>
        /// <param name="targetButtonNumber">
        /// Identifies the image.
        /// </param>		
        private void Program_onTargetButtonTouch(Int32 targetButtonNumber)
        {
            try
            {
                Int32 gpsTimeoutSeconds = 5;

                // myComPort.OpenComPort();
                //  ClearStatusText();

                //  targetTypeSelected = myTarget.targetType[targetButtonNumber];
                //  targetButtonSelected = targetButtonNumber;
                //  AddStatusText(myTarget.targetType[targetButtonNumber], displayFont, textColor, true);

                //  AddStatusText(" ", displayFont, textColor, true);
                //  AddStatusText("Waiting for GPS data...", displayFont, textColor, true);

                //  // Start a timer to time out if the GPS unit sends no data.

                //  gpsTimeout.Interval = new TimeSpan(0, 0, gpsTimeoutSeconds);
                //  gpsTimeout.Tick += new EventHandler(GpsTimeout_Tick);
                //  gpsTimeout.Start();
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }

        /// <summary>
        /// Set parameters for displaying target buttons and labels.
        /// Assign an event handler.
        /// </summary>		
        private void SetDisplayParametersForTargetImages()
        {
            try
            {
                Int32 captionSpacing = 0;
                Int32 horizontalSpacing = 16;
                Int32 topMargin = 12;

                for (Int32 i = 0; i < Targets.numberOfTargetButtons; i++)
                {
                    myTargetButtons[i] = new TargetButtons(i);
                    myTargetButtons[i].HorizontalAlignment = HorizontalAlignment.Center;
                    myTargetButtons[i].onTargetButtonTouch += Program_onTargetButtonTouch;
                    myTargetButtons[i].SetMargin(horizontalSpacing, topMargin, horizontalSpacing, captionSpacing);
                    myTargetButtons[i].VerticalAlignment = VerticalAlignment.Top;
                    myTargetButtons[i].Width = 64;
                }

                for (Int32 i = 0; i < targetLabels.Length; i++)
                {
                    targetLabels[i] = new Text();
                    targetLabels[i].Font = displayFont;
                    targetLabels[i].ForeColor = textColor;
                    targetLabels[i].HorizontalAlignment = HorizontalAlignment.Center;
                    targetLabels[i].SetMargin(horizontalSpacing, captionSpacing, horizontalSpacing, 0);
                    targetLabels[i].TextAlignment = TextAlignment.Center;
                    targetLabels[i].TextContent = myTarget.targetType[i];
                    targetLabels[i].TextWrap = true;
                    targetLabels[i].VerticalAlignment = VerticalAlignment.Top;
                    targetLabels[i].Width = 64;

                    // Set a height so the targetCounts line up horizontally.
                    // Otherwise, each targetCount lies immediately below its targetLabel, 
                    // and if the targetLabels vary in how many lines they use, the counts don't line up horizontally.

                    targetLabels[i].Height = 32;
                }

                for (Int32 i = 0; i < targetCount.Length; i++)
                {
                    targetCount[i] = new Text();
                    targetCount[i].Font = displayFont;
                    targetCount[i].ForeColor = textColor;
                    targetCount[i].HorizontalAlignment = HorizontalAlignment.Center;
                    targetCount[i].SetMargin(horizontalSpacing, captionSpacing, horizontalSpacing, 0);
                    targetCount[i].TextAlignment = TextAlignment.Center;
                    targetCount[i].TextContent = "0";
                    targetCount[i].TextWrap = true;
                    targetCount[i].VerticalAlignment = VerticalAlignment.Top;
                }
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }

        /// <summary>
        /// Set the display parameters for the Undo button and label.
        /// Assign an event handler.
        /// </summary>		
        //private void SetDisplayParametersForUndoButton()
        //{
        //    try
        //    {
        //        myUndoButton.HorizontalAlignment = HorizontalAlignment.Right;
        //        myUndoButton.onUndoButtonTouch += new UndoButton.ButtonTouchedHandler(Program_onUndoButtonTouch);
        //        myUndoButton.SetMargin(4);
        //        myUndoButton.VerticalAlignment = VerticalAlignment.Bottom;
        //        undoText.ForeColor = textColor;
        //        undoText.HorizontalAlignment = HorizontalAlignment.Center;
        //        undoText.SetMargin(2);
        //        undoText.TextAlignment = TextAlignment.Center;
        //        undoText.TextWrap = true;
        //        undoText.VerticalAlignment = VerticalAlignment.Bottom;
        //    }
        //    catch (Exception ex)
        //    {
        //        Debug.Print(ex.Message);
        //    }
        //}
        /// <summary>
        /// Place items in the StackPanels. Add the StackPanels to the main Panel.
        /// </summary>		
        /// <remarks>
        /// Edit this code if the number of logging targets changes.
        /// </remarks> 
        private void PlaceItemsInPanels()
        {
            try
            {
                // Add a button and label in each target StackPanel.

                target1.Children.Add(myTargetButtons[0]);
                target1.Children.Add(targetLabels[0]);
                target1.Children.Add(targetCount[0]);

                target2.Children.Add(myTargetButtons[1]);
                target2.Children.Add(targetLabels[1]);
                target2.Children.Add(targetCount[1]);

                // The target StackPanels reside within the imagePanel Stackpanel.				

                imagePanel.Children.Add(target1);
                imagePanel.Children.Add(target2);


                //  statusPanel.Children.Add(statusText);

                // Add the StackPanels to the main panel.	
                // The order is critical.

                // mainPanel.Children.Add(statusPanel);
                mainPanel.Children.Add(imagePanel);
                // mainPanel.Children.Add(undoPanel);
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
        }


        public Window CreateWindow()
        {
            try
            {
                mainWindow = new Window();
                mainWindow.Height = SystemMetrics.ScreenHeight;
                mainWindow.Width = SystemMetrics.ScreenWidth;

                mainWindow.Visibility = Visibility.Visible;
                mainWindow.Dispatcher.Thread.Priority = ThreadPriority.Highest;
                mainWindow.Background = new SolidColorBrush(Color.Black);

                SetDisplayParametersForTargetImages();
                // SetDisplayParametersForUndoButton();
                SetDisplayParametersForStatusText();

                PlaceItemsInPanels();
                mainWindow.Child = mainPanel;

                return mainWindow;
            }
            catch (Exception ex)
            {
                Debug.Print(ex.Message);
                return mainWindow;
            }
        }
    }
}